package com.jiantong.web.open.app.work;

import com.google.common.collect.Maps;
import com.jiantong.user.service.DepartmentExtendReadService;
import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.model.BreedReportsForm;
import com.jiantong.work.service.BreedReportsFormReadService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.web.bind.annotation.RequestMethod;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/4/11.
 *
 */
@Slf4j
@OpenBean
public class BreedReportsFormApis {

    private static final DateTimeFormatter DTF = DateTimeFormat.forPattern("yyyy-MM-dd");

    @RpcConsumer
    private BreedReportsFormReadService breedReportsFormReadService;

    @RpcConsumer
    private DepartmentExtendReadService departmentExtendReadService;

    @OpenMethod(
            key = "find.sum.order.by.breed.type",
            paramNames = {"fromDate","toDate","breedFactoryId"},
            httpMethods = RequestMethod.GET
    )
    public List<Map<String , BigDecimal>> findSumGroupByBreedType(String fromDate , String toDate,Long breedFactoryId){
        Date start = null;
        if (!Strings.isNullOrEmpty(fromDate)) {
            start = DateTime.parse(fromDate).toDate();
        }

        Date end = null;
        if (!Strings.isNullOrEmpty(toDate)) {
            end = DateTime.parse(toDate).toDate();
        }
        Response<List<Map<String , BigDecimal>>> listResponse = breedReportsFormReadService.findSumGroupByBreedType(start,end,breedFactoryId);
        if(!listResponse.isSuccess()){
            log.error("failed to find.SumGroupByBreedType, cause : {}", listResponse.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    listResponse.getError());
        }
        return listResponse.getResult();
    }

    @OpenMethod(
            key = "find.sum.breed.report.form",
            paramNames = {"fromDate","toDate","breedType"},
            httpMethods = RequestMethod.GET
    )
    public List<Map<String , BigDecimal>> findSum(String fromDate , String toDate ,Integer breedType){
        Date start = null;
        if (!Strings.isNullOrEmpty(fromDate)) {
            start = DateTime.parse(fromDate).toDate();
        }

        Date end = null;
        if (!Strings.isNullOrEmpty(toDate)) {
            end = DateTime.parse(toDate).toDate();
        }
        Response<List<Map<String , BigDecimal>>> listResponse = breedReportsFormReadService.findSum(start,end,breedType);
        if(!listResponse.isSuccess()){
            log.error("failed to find.sum normal , cause : {}", listResponse.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    listResponse.getError());
        }
        return listResponse.getResult();
    }

    @OpenMethod(
            key = "find.sum.by.is.to.local",
            paramNames = {"fromDate","toDate","breedType"},
            httpMethods = RequestMethod.GET
    )
    public Map<String , Integer> findSumByIsToLocal(String fromDate , String toDate , Integer breedType){
        Date start = null;
        if (!Strings.isNullOrEmpty(fromDate)) {
            start = DateTime.parse(fromDate).toDate();
        }

        Date end = null;
        if (!Strings.isNullOrEmpty(toDate)) {
            end = DateTime.parse(toDate).toDate();
        }
        Response<List<Map<String , Integer>>> listResponse = breedReportsFormReadService.findSumByIsToLocal(start,end,breedType);
        if(!listResponse.isSuccess()){
            log.error("failed to find.sum normal , cause : {}", listResponse.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    listResponse.getError());
        }
        Map<String , Integer> result = Maps.newHashMap();
        for(Map<String ,Integer> map : listResponse.getResult()){
            if(map.get("isToLocal") == 1){
                result.put("local",map.get("qualifiedQuantity"));
            }else{
                result.put("nonLocal",map.get("qualifiedQuantity"));
            }
        }
        return result;
    }

    @OpenMethod(
            key = "find.sum.group.by.butcher.factory",
            paramNames = {"fromDate","toDate","breedType"},
            httpMethods = RequestMethod.GET
    )
    public List<Map<String , BigDecimal>> findSumGroupByButcherFactory(String fromDate , String toDate ,Integer breedType){
        Date start = null;
        if (!Strings.isNullOrEmpty(fromDate)) {
            start = DateTime.parse(fromDate).toDate();
        }

        Date end = null;
        if (!Strings.isNullOrEmpty(toDate)) {
            end = DateTime.parse(toDate).toDate();
        }
        Response<List<Map<String , BigDecimal>>> listResponse = breedReportsFormReadService.findSumGroupByButcherFactory(start,end,breedType);
        if(!listResponse.isSuccess()){
            log.error("failed to find.sum normal , cause : {}", listResponse.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    listResponse.getError());
        }
        return listResponse.getResult();
    }

    @OpenMethod(
            key = "find.sum.group.by.breed.factory.town",
            paramNames = {"fromDate","toDate","breedType"},
            httpMethods = RequestMethod.GET
    )
    public List<Map<String , BigDecimal>> findSumGroupByBreedFactoryTown(String fromDate , String toDate ,Integer breedType){
        Date start = null;
        if (!Strings.isNullOrEmpty(fromDate)) {
            start = DateTime.parse(fromDate).toDate();
        }

        Date end = null;
        if (!Strings.isNullOrEmpty(toDate)) {
            end = DateTime.parse(toDate).toDate();
        }
        Response<List<Map<String , BigDecimal>>> listResponse = breedReportsFormReadService.findSumGroupByBreedFactoryTown(start,end ,breedType);
        if(!listResponse.isSuccess()){
            log.error("failed to find.sum normal , cause : {}", listResponse.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    listResponse.getError());
        }
        return listResponse.getResult();
    }

    @OpenMethod(
            key = "paging.breed.reports.form",
            paramNames = {"pageNo","pageSize","fromDate","toDate","breedType"},
            httpMethods = RequestMethod.GET
    )
    public Paging<BreedReportsForm> pagingBreedReportsForm(Integer pageNo , Integer pageSize ,String fromDate, String toDate, Integer breedType){

        Date start = null;
        if (!Strings.isNullOrEmpty(fromDate)) {
            start = DateTime.parse(fromDate).toDate();
        }

        Date end = null;
        if (!Strings.isNullOrEmpty(toDate)) {
            end = DateTime.parse(toDate).toDate();
        }
        Response<Paging<BreedReportsForm>> response =
                breedReportsFormReadService.pagingBreedReportsForm(pageNo,pageSize,start,end,breedType);
        if(!response.isSuccess()){
            log.error("failed to paging.BreedReportsForm, cause : {}", response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "find.breed.reports.form",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public BreedReportsForm findBreedReportsFormById(Long id){
        if(id == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<BreedReportsForm> reportsFormResponse = breedReportsFormReadService.findById(id);
        if(!reportsFormResponse.isSuccess()){
            log.error("failed to find.BreedReportsForm, cause : {}", reportsFormResponse.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    reportsFormResponse.getError());
        }
        return reportsFormResponse.getResult();
    }
}
